package com.yourvote.dao.impl;

import com.yourvote.dao.Dao;
import com.yourvote.dao.DistrictDao;
import com.yourvote.model.District;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: pavlo.fedyna
 * Date: 10/25/12
 * Time: 12:43 PM
 * To change this template use File | Settings | File Templates.
 */
public class DistrictDaoImpl extends BaseDaoImpl implements DistrictDao{
    @Override
    public EntityManager getEM() {
        return Dao.INSTANCE.getEm();
    }

    @Override
    public List<District> getAll() {
        return (List<District>) getAll("select d from District d");
    }

    @Override
    public void add(District district) {
        persist(district);
    }

    @Override
    public District getDistrict(int id) {
        Query q = getEM().createQuery("select d from District d where d.number = :district");
        q.setParameter("district", id);
        return (District) q.getSingleResult();
    }

    @Override
    public void remove(District district) {
        District district1 = getEM().find(District.class,district);
        getEM().remove(district1);
    }
}
